#include "../inc/keyBoard.h"



/**
 * @brief 从触摸坐标获取按键输入并处理用户登录/注册逻辑
 *
 * 该函数实现了一个交互式键盘界面，主要功能包括：
 * 1. 显示登录界面和虚拟键盘
 * 2. 根据触摸坐标识别数字、字母和功能键输入
 * 3. 管理用户名和密码的输入、删除和大小写切换
 * 4. 处理注册和登录操作
 * 5. 在LCD屏幕上实时显示输入内容
 *
 * 函数流程：
 * - 初始化界面和缓冲区
 * - 进入循环等待触摸事件
 * - 根据坐标区域判断输入类型
 * - 更新临时缓冲区并显示
 * - 处理功能键(删除、确认、大小写切换)
 * - 执行注册/登录操作
 *
 * @return int 登录成功时返回3，其他情况无返回值
 *
 * @note
 * - 使用静态缓冲区存储用户名和密码
 * - 坐标范围定义在NumKeys和AlphaKeys枚举中
 * - 依赖BMP类显示界面，Lcd类控制显示设备
 * - 通过userManager处理用户认证
 */
std::string getKeyFromCoordinates()
{
    // 账号文本宽位置：340, 50，密码文本框位置：340，130
    int textWidth = 340,textHeight = 50;//用于改变文本宽的位置

    int change = 0;//用于改变大小写字母
    static char username[20]={0};//用于存储账号名
    static char password[20]={0};//用于存储密码
    std::string tempPassword;//用于存储临时账号
    char tempUsername[20]={0};//用于存储临时账号名和密码
    
    char registerSuccess[20] = {0};//用于打印注册成功跟注册失败
    
    int num = 0;//用于记录当前数值
    int unum =0;//用于记录账号数值
    int pnum =0;//用于记录密码数值

    int isUsername = 1;// 用于判断存入账号还是密码

    // 显示登录界面
    BMP login("./mainInterface/login.bmp");
    BMP keyBoard("./mainInterface/keyBoard.bmp");
    BMP keyBoardUp("./mainInterface/keyBoardUp.bmp");
    BMP keyBoardLow("./mainInterface/keyBoardLow.bmp");

    // 创建滑动检测器实例，获取触摸事件坐标
    SwipeDetector detector;

    //创建ShowFont对象
    ShowFont* keyshowFont = ShowFont::instance();

    login.show(Lcd::instance()->addr(), 0, 0);

    while (1)
    {
        // 获取触摸事件坐标
        detector.detectSwipe();
        std::cout<<"x = "<<detector.getX()<<" "<<"y = "<<detector.getY()<<std::endl;
        
        // 检查数字键
        if (detector.getX() >= static_cast<int>(NumKeys::N0_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N0_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N0_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N0_MAX_Y)) 
        {
            tempUsername[num++] = '0';
            cout << "0" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N1_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N1_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N1_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N1_MAX_Y)) 
        {
            tempUsername[num++] = '1';
            cout << "1" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N2_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N2_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N2_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N2_MAX_Y))
        {
            tempUsername[num++] = '2';
            cout << "2" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N3_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N3_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N3_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N3_MAX_Y))
        {
            tempUsername[num++] = '3';
            cout << "3" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N4_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N4_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N4_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N4_MAX_Y))
        {
            tempUsername[num++] = '4';
            cout << "4" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N5_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N5_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N5_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N5_MAX_Y))
        {
            tempUsername[num++] = '5';
            cout << "5" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N6_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N6_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N6_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N6_MAX_Y))
        {
            tempUsername[num++] = '6';
            cout << "6" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N7_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N7_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N7_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N7_MAX_Y))
        {
            tempUsername[num++] = '7';
            cout << "7" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N8_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N8_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N8_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N8_MAX_Y))
        {
            tempUsername[num++] = '8';
            cout << "8" << endl;
        }
        else if (detector.getX() >= static_cast<int>(NumKeys::N9_MIN_X) && detector.getX() <= static_cast<int>(NumKeys::N9_MAX_X) &&
                 detector.getY() >= static_cast<int>(NumKeys::N9_MIN_Y) && detector.getY() <= static_cast<int>(NumKeys::N9_MAX_Y))
        {
            tempUsername[num++] = '9';
            keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);
            cout << "9" << endl;
        }

        // 检查字母键
        else if (detector.getX() >= static_cast<int>(AlphaKeys::A_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::A_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::A_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::A_MAX_Y))
        {
            tempUsername[num++] = 'a'-change;
            cout << "a" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::B_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::B_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::B_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::B_MAX_Y))
        {
            tempUsername[num++] = 'b'-change;
            cout << "b" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::C_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::C_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::C_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::C_MAX_Y))
        {
            tempUsername[num++] = 'c'-change;
            cout << "c" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::D_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::D_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::D_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::D_MAX_Y))
        {
            tempUsername[num++] = 'd'-change;
            cout << "d" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::E_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::E_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::E_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::E_MAX_Y))
        {
            tempUsername[num++] = 'e'-change;
            cout << "e" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::F_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::F_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::F_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::F_MAX_Y))
        {
            tempUsername[num++] = 'f'-change;
            cout << "f" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::G_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::G_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::G_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::G_MAX_Y))
        {
            tempUsername[num++] = 'g'-change;
            cout << "g" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::H_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::H_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::H_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::H_MAX_Y))
        {
            tempUsername[num++] = 'h'-change;
            cout << "h" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::I_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::I_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::I_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::I_MAX_Y))
        {
            tempUsername[num++] = 'i'-change;
            cout << "i" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::J_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::J_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::J_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::J_MAX_Y))
        {
            tempUsername[num++] = 'j'-change;
            cout << "j" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::K_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::K_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::K_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::K_MAX_Y))
        {
            tempUsername[num++] = 'k'-change;
            cout << "k" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::L_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::L_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::L_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::L_MAX_Y))
        {
            tempUsername[num++] = 'l'-change;
            cout << "l" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::M_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::M_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::M_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::M_MAX_Y))
        {
            tempUsername[num++] = 'm'-change;
            cout << "m" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::N_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::N_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::N_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::N_MAX_Y))
        {
            tempUsername[num++] = 'n'-change;
            cout << "n" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::O_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::O_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::O_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::O_MAX_Y))
        {
            tempUsername[num++] = 'o'-change;
            cout << "o" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::P_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::P_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::P_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::P_MAX_Y))
        {
            tempUsername[num++] = 'p'-change;
            cout << "p" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::Q_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::Q_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::Q_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::Q_MAX_Y))
        {
            tempUsername[num++] = 'q'-change;
            cout << "q" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::R_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::R_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::R_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::R_MAX_Y))
        {
            tempUsername[num++] = 'r'-change;
            cout << "r" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::S_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::S_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::S_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::S_MAX_Y))
        {
            tempUsername[num++] = 's'-change;
            cout << "s" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::T_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::T_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::T_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::T_MAX_Y))
        {
            tempUsername[num++] = 't'-change;
            cout << "t" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::U_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::U_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::U_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::U_MAX_Y))
        {
            tempUsername[num++] = 'u'-change;
            cout << "u" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::V_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::V_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::V_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::V_MAX_Y))
        {
            tempUsername[num++] = 'v'-change;
            cout << "v" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::W_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::W_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::W_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::W_MAX_Y))
        {
            tempUsername[num++] = 'w'-change;
            cout << "w" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::X_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::X_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::X_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::X_MAX_Y))
        {
            tempUsername[num++] = 'x'-change;
            cout << "x" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::Y_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::Y_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::Y_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::Y_MAX_Y))
        {
            tempUsername[num++] = 'y'-change;
            cout << "y" << endl;
        }
        else if (detector.getX() >= static_cast<int>(AlphaKeys::Z_MIN_X) && detector.getX() <= static_cast<int>(AlphaKeys::Z_MAX_X) &&
                 detector.getY() >= static_cast<int>(AlphaKeys::Z_MIN_Y) && detector.getY() <= static_cast<int>(AlphaKeys::Z_MAX_Y))
        {
            tempUsername[num++] = 'z'-change;
            cout << "z" << endl;
        }

        // 检查功能键
        else if (detector.getX() >= static_cast<int>(FunctionKeys::Delete_MIN_X) && detector.getX() <= static_cast<int>(FunctionKeys::Delete_MAX_X) &&
                 detector.getY() >= static_cast<int>(FunctionKeys::Delete_MIN_Y) && detector.getY() <= static_cast<int>(FunctionKeys::Delete_MAX_Y))
        {
            //删除上一个字符
            if (num > 0)
            {
                tempUsername[--num]='\0';
            }
            keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);
            cout << "Delete" << endl;
        }
        else if (detector.getX() >= static_cast<int>(FunctionKeys::Enter_MIN_X) && detector.getX() <= static_cast<int>(FunctionKeys::Enter_MAX_X) &&
                 detector.getY() >= static_cast<int>(FunctionKeys::Enter_MIN_Y) && detector.getY() <= static_cast<int>(FunctionKeys::Enter_MAX_Y))
        {
            //判断是在账号框输入还是密码框输入
            if(isUsername==1)
            {
                //转到密码框
                isUsername = 0;
                //清除tempUsername里面的内容
                memset(tempUsername, 0, sizeof(tempUsername));
                //把num标志位清0
                num = 0;
                //把账号框隐藏
                keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);
                //显示密码框
                textHeight=130;
            }
            else if(isUsername==0)
            {
                //把tempUsernam存入密码
                strcpy(password,tempUsername);
                //清除tempUsername里面的内容
                memset(tempUsername, 0, sizeof(tempUsername));
                num = 0;
            }
            
            keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);
            cout << "Enter" << endl;
        }
        else if (detector.getX() >= static_cast<int>(FunctionKeys::LowerCase_MIN_X) && detector.getX() <= static_cast<int>(FunctionKeys::LowerCase_MAX_X) &&
                 detector.getY() >= static_cast<int>(FunctionKeys::LowerCase_MIN_Y) && detector.getY() <= static_cast<int>(FunctionKeys::LowerCase_MAX_Y))
        {
            keyBoardLow.show(Lcd::instance()->addr(), 0, 240);
            change = 0;
            cout << "LowerCase" << endl;
        }
        else if (detector.getX() >= static_cast<int>(FunctionKeys::UpperCase_MIN_X) && detector.getX() <= static_cast<int>(FunctionKeys::UpperCase_MAX_X) &&
                 detector.getY() >= static_cast<int>(FunctionKeys::UpperCase_MIN_Y) && detector.getY() <= static_cast<int>(FunctionKeys::UpperCase_MAX_Y))
        {
            keyBoardUp.show(Lcd::instance()->addr(), 0, 240);
            change = 32;
            cout << "UpperCase" << endl;
        }
        
        // 检查是在账号框输入还是密码框输入
        if(isUsername==1)
        {
            strcpy(username,tempUsername);//把tempUsername存入账号并且转到密码框   
            unum=num;//账号下标等于tempUsername的下标
            keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);
        }
        else if(isUsername==0)
        {
            strcpy(password,tempUsername);//把tempUsernam存入密码   
            pnum=num;//账号下标等于tempUsername的下标
            keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);
        }
    
        // 上方向框按钮        
        if (detector.getX() >= 340 && detector.getX() <= 520 && detector.getY() >= 45 && detector.getY() <= 95)//账号框位置
        {
            std::cout << "账号" << std::endl;
            textHeight = 50;//账号框位置
            isUsername = 1; //密码存入账号
            change = 0;     //恢复默认小写字母
            num = unum;     //num下标等于账号下标
            //清除tempUsername里面的内容,strcpy清除不了tempUsername里面的内容
            memset(tempUsername, 0, sizeof(tempUsername));
            
            keyBoard.show(Lcd::instance()->addr(), 0, 240);//显示键盘框
            
            //如果账号内容不为空，把账号内容存入tempUsername
            if (strlen(username) != 0)
            { 
                strcpy(tempUsername,username);//把账号内容存入tempUsername
            }
        
            //显示键盘输入文本框
            keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);
        }
        else if (detector.getX() >= 340 && detector.getX() <= 520 && detector.getY() >= 105 && detector.getY() <= 165)//密码框位置
        {   
            std::cout << "密码" << std::endl;
            textHeight = 130;//密码框位置
            isUsername = 0;  //密码存入账号
            change = 0;      //恢复默认小写字母
            num = pnum;      //num下标等于密码下标
            //清除tempUsername里面的内容,strcpy清除不了tempUsername里面的内容
            memset(tempUsername, 0, sizeof(tempUsername));

            keyBoard.show(Lcd::instance()->addr(), 0, 240);//显示键盘输入框

            //如果密码内容不为空，把密码内容存入tempUsername
            if (strlen(password) != 0)
            {
                strcpy(tempUsername,password);//把密码内容存入tempUsername
            } 
            
            //显示键盘输入文本框
            keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);
        }
        else if (detector.getX() >= 245 && detector.getX() <= 325 && detector.getY() >= 180 && detector.getY() <= 230)//注册
        {
            //如果密码或者账号里面的内容为空，就不让注册
            if (strlen(password) == 0 || strlen(username) == 0)
            {
                login.show(Lcd::instance()->addr(), 0, 0);//显示登录界面
                strcpy(registerSuccess, " 注册失败");//注册失败提示
                keyshowFont->display(registerSuccess, 46, 160, 50, getColor(0,86, 64, 130), ShowFont::white, 310, 260);
            }
            //把账号跟密码存入user文件
            else if(UserManager::getInstance().createUser(username, password))
            {
                UserManager::getInstance().saveUsers();// 注册成功后立即保存数据
                login.show(Lcd::instance()->addr(), 0, 0);//显示登录界面
                strcpy(registerSuccess, " 注册成功");//注册成功提示
                keyshowFont->display(registerSuccess, 46, 160, 50, getColor(0,86, 64, 130), ShowFont::white, 310, 260);

                // userManager.updateUserScore(username,"Snake",0); // 更新分数
                // userManager.updateUserScore(username,"2048",0); // 更新分数
            }

            //清除用户、密码、tempUsername里面的内容
            memset(username, 0, sizeof(username));
            memset(password, 0, sizeof(password));
            memset(tempUsername, 0, sizeof(tempUsername));
            num = 0;
            unum= 0;
            pnum= 0;
        }
        else if (detector.getX() >= 440 && detector.getX() <= 520 && detector.getY() >= 190 && detector.getY() <= 225)//登录
        {
            //判断账号密码是否正确
            if (UserManager::getInstance().authenticate(username, password))
            {
                //把账号存入临时账号，用于返回
                tempPassword = username;
                //std::cout<<"用户贪吃蛇游戏分数"<<userManager.getUserHighScore(tempPassword ,"Snake")<<std::endl;

                //清除用户、密码、tempUsername里面的内容
                memset(username, 0, sizeof(username));
                memset(password, 0, sizeof(password));
                memset(tempUsername, 0, sizeof(tempUsername));
                num = 0;
                unum= 0;
                pnum= 0;

                //显示键盘输入文本框
                keyshowFont->display(tempUsername, 32, 200, 35, getColor(0,86, 64, 130), ShowFont::white, textWidth, textHeight);

                //返回登录的用户名
                return tempPassword;
            }
            else
            {
                login.show(Lcd::instance()->addr(), 0, 0);//显示登录界面
                strcpy(registerSuccess, " 登录失败");//登录失败提示
                keyshowFont->display(registerSuccess, 46, 160, 50, getColor(0,86, 64, 130), ShowFont::white, 310, 260);
                //清除用户、密码、tempUsername里面的内容
                memset(username, 0, sizeof(username));
                memset(password, 0, sizeof(password));
                memset(tempUsername, 0, sizeof(tempUsername));
                num = 0;
                unum= 0;
                pnum= 0;
            }
        }
        else if (detector.getX() >= 700 && detector.getX() <= 800 && detector.getY() >= 0 && detector.getY() <= 60)//退出登录界面
        {
            //清除用户、密码、tempUsername里面的内容
            memset(username, 0, sizeof(username));
            memset(password, 0, sizeof(password));
            memset(tempUsername, 0, sizeof(tempUsername));
            num = 0;
            unum= 0;
            pnum= 0;
            return "";
        }
        else if (detector.getX() >= 530 && detector.getX() <= 800 && detector.getY() >= 0 && detector.getY() <= 240)//空白界面
        {
            login.show(Lcd::instance()->addr(), 0, 0);//显示登录界面
        }
        else if (detector.getX() >= 0   && detector.getX() <= 250 && detector.getY() >= 0 && detector.getY() <= 240)//空白界面
        {
            login.show(Lcd::instance()->addr(), 0, 0);//显示登录界面
        }

    }
}
